﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NRemedy.Linq;
using BMC.ARSystem;
using System.Linq.Expressions;
using NRemedy.Test.Util;
using System.Reflection;

/*
 
 * Test for linq to NRemedy
 * The following ar data type is support in linq's where clause
 * +0:Character
 * +1:Interger
 * 2:Real
 * +3:Decimal
 * 4:DropDownListField
 * 5:RadioButtonField
 * +6:CheckBoxField
 * 
 * The following ar data type may support in future version
 * DateTimeField
 * DateField
 
 */

namespace NRemedy.Test
{
//new RegularForm{ Submitter = "Parker", CheckBoxField = RegularForm.CheckBoxField_Enum.Default1, IntegerField = 1,DecimalNumberField = 1.0m },
//new RegularForm{ Submitter = "Parker", CheckBoxField = RegularForm.CheckBoxField_Enum.Default2, IntegerField = 2,DecimalNumberField = 2.0m },
//new RegularForm{ Submitter = "Parker2", CheckBoxField = RegularForm.CheckBoxField_Enum.Default2, IntegerField = 3,DecimalNumberField = 3.0m },
//new RegularForm{ Submitter = "Parker2", CheckBoxField = RegularForm.CheckBoxField_Enum.Default1, IntegerField = 4,DecimalNumberField = 4.0m },
//new RegularForm{ Submitter = "Parker", CheckBoxField = RegularForm.CheckBoxField_Enum.Default1, IntegerField = 5,DecimalNumberField = 5.0m },
//new RegularForm{ Submitter = "Parker", CheckBoxField = RegularForm.CheckBoxField_Enum.Default1, IntegerField = 6,DecimalNumberField = 6.0m }

    [TestClass]
    public class LinqToAR
    {
        /*
         
         * where clause test below
         
         */
        [TestMethod]//no where clause
        public void LinqToAR_Where_Clause_No_Where()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
            var q = from s in rgf
                    select s;
            List<RegularForm> result = q.ToList();
            Assert.AreEqual(6, result.Count);
        }

        [TestMethod]//Character where clause
        public void LinqToAR_Where_Clause_Character_Clause()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
            var q = from s in rgf
                    where s.Submitter == "Parker"
                    select s;
            List<RegularForm> result = q.ToList();
            Assert.AreEqual(4, result.Count);
            foreach (var item in result)
            {
                Assert.AreEqual("Parker", item.Submitter);
            }
        }

        [TestMethod]//Enum where clause
        public void LinqToAR_Where_Clause_Enum_Clause()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
            var q = from s in rgf
                    where s.CheckBoxField == RegularForm.CheckBoxField_Enum.Default2
                    select s;
            List<RegularForm> result = q.ToList();
            Assert.AreEqual(2, result.Count);
            foreach (var item in result)
            {
                Assert.AreEqual(RegularForm.CheckBoxField_Enum.Default2, item.CheckBoxField);
            }
        }

        [TestMethod]//Integer where clause
        public void LinqToAR_Where_Clause_Integer_Clause()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
            var q = from s in rgf
                    where s.IntegerField > 3
                    select s;
            List<RegularForm> result = q.ToList();
            Assert.AreEqual(3, result.Count);
            foreach (var item in result)
            {
                Assert.AreEqual(true, item.IntegerField > 3);
            }
        }

        [TestMethod]//Decimal where clause
        public void LinqToAR_Where_Clause_Decimal_Clause()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
            var q = from s in rgf
                    where s.DecimalNumberField >= 3m
                    select s;
            List<RegularForm> result = q.ToList();
            Assert.AreEqual(4, result.Count);
            foreach (var item in result)
            {
                Assert.AreEqual(true, item.DecimalNumberField >= 3m);
            }
        }

        [TestMethod]//null where clause
        public void LinqToAR_Where_Clause_NULL_Clause()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
            var q = from s in rgf
                    where s.CharacterField == null
                    select s;
            List<RegularForm> result = q.ToList();
            Assert.AreEqual(6, result.Count);
            foreach (var item in result)
            {
                Assert.AreEqual(null, item.CharacterField);
            }
        }

        [TestMethod]//combine where clause
        public void LinqToAR_Where_Clause_Combine_Clause1()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
            var q = from s in rgf
                    where s.DecimalNumberField >= 3m && s.Submitter == "Parker"
                    select s;
            List<RegularForm> result = q.ToList();
            Assert.AreEqual(2, result.Count);
            foreach (var item in result)
            {
                Assert.AreEqual(true, item.DecimalNumberField >= 3m);
                Assert.AreEqual("Parker", item.Submitter);
            }
        }

        [TestMethod]//combine where clause
        public void LinqToAR_Where_Clause_Combine_Clause2()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
            var q = from s in rgf
                    where s.DecimalNumberField >= 3m && (s.Submitter == "Parker" || s.CheckBoxField == RegularForm.CheckBoxField_Enum.Default2)
                    select s;
            List<RegularForm> result = q.ToList();
            Assert.AreEqual(3, result.Count);
            foreach (var item in result)
            {
                Assert.AreEqual(true, item.DecimalNumberField >= 3m);
                Assert.AreEqual(true, item.Submitter == "Parker" || item.CheckBoxField == RegularForm.CheckBoxField_Enum.Default2);
            }
        }

        [TestMethod]//where clause
        public void LinqToAR_Where_Clause_with_local_reference_1()
        {
            //ignore
            EE e = new EE();
            e.str = "Parker";
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
            var q = from s in rgf
                    where s.DecimalNumberField >= 3m && (s.Submitter == e.str || s.CheckBoxField == RegularForm.CheckBoxField_Enum.Default2)
                    select s;
            List<RegularForm> result = q.ToList();
            Assert.AreEqual(3, result.Count);
            foreach (var item in result)
            {
                Assert.AreEqual(true, item.DecimalNumberField >= 3m);
                Assert.AreEqual(true, item.Submitter == e.str || item.CheckBoxField == RegularForm.CheckBoxField_Enum.Default2);
            }
        }

        public class EE
        {
            public string str;
            public decimal deci;
        }

        [TestMethod]//where clause
        public void LinqToAR_Where_Clause_with_local_reference_2()
        {
            //ignore
            EE e = new EE();
            e.deci = 3m;
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
            var q = from s in rgf
                    where s.DecimalNumberField >= e.deci && (s.Submitter == "Parker" || s.CheckBoxField == RegularForm.CheckBoxField_Enum.Default2)
                    select s;
            List<RegularForm> result = q.ToList();
            Assert.AreEqual(3, result.Count);
            foreach (var item in result)
            {
                Assert.AreEqual(true, item.DecimalNumberField >= 3m);
                Assert.AreEqual(true, item.Submitter == "Parker" || item.CheckBoxField == RegularForm.CheckBoxField_Enum.Default2);
            }
        }

        [TestMethod]//where clause
        public void LinqToAR_Where_Clause_with_datetime_equal()
        {
            //ignore
            EE e = new EE();
            e.deci = 3m;
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
            var q = from s in rgf
                    where s.DateTimeField == new DateTime(2012,3,1)
                    select s;
            List<RegularForm> result = q.ToList();
            Assert.AreEqual(2, result.Count);
        }

        [TestMethod]//where clause
        public void LinqToAR_Where_Clause_with_datetime_less()
        {
            //ignore
            EE e = new EE();
            e.deci = 3m;
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
            var q = from s in rgf
                    where s.DateTimeField < new DateTime(2012, 12, 21)
                    select s;
            List<RegularForm> result = q.ToList();
            Assert.AreEqual(5, result.Count);
        }

        /*
         
         * count clause test below
         
         */


        [TestMethod]//count clause
        public void LinqToAR_Count_Clause_With_Where_Clause()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
            var q = from s in rgf
                    where s.DecimalNumberField >= 3m && (s.Submitter == "Parker" || s.CheckBoxField == RegularForm.CheckBoxField_Enum.Default2)
                    select s;
            Assert.AreEqual(3, q.Count());
        }

        [TestMethod]//count clause
        public void LinqToAR_Count_Clause_With_Where_Clause2()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
            var q = from s in rgf
                    select s;
            Assert.AreEqual(6, q.Count());
        }  

        [TestMethod]//count clause do not support condition
        [ExpectedException(typeof(NotSupportedException))]
        public void LinqToAR_Count_Clause_With_Condition()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
            var q = from s in rgf
                    select s;
            q.Count(s => s.DecimalNumberField >= 3m && (s.Submitter == "Parker" || s.CheckBoxField == RegularForm.CheckBoxField_Enum.Default2));
        }

        /*
         
   * select clause test below
         
   */

        [TestMethod]//select clause
        public void LinqToAR_Count_Clause_With_Select_Clause()
        {
            //ignore
            using (ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin"))
            {
                ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
                var q = from s in rgf
                        where s.Submitter == "Parker"
                        select s.Submitter;

                var result = q.ToList();
                Assert.AreEqual(4, result.Count);
                foreach (var r in result)
                {
                    Assert.AreEqual(true, r == "Parker");
                }
            }
        }

        [TestMethod]//select clause
        public void LinqToAR_Count_Clause_With_Select_Clause2()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
            var q = from s in rgf
                    where s.CheckBoxField == RegularForm.CheckBoxField_Enum.Default2
                    select s.CheckBoxField;

            var result = q.ToList();
            Assert.AreEqual(2, result.Count);
            foreach (var r in result)
            {
                Assert.AreEqual(true, r == RegularForm.CheckBoxField_Enum.Default2);
            }
        }

        [TestMethod]//select clause
        public void LinqToAR_Count_Clause_With_Select_Clause3()
        {
            //ignore
            using (ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin"))
            {
                ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
                var q = from s in rgf
                        where s.CheckBoxField == RegularForm.CheckBoxField_Enum.Default2
                        && s.IntegerField == 2
                        select new
                        {
                            s.Submitter,
                            s.CheckBoxField,
                            s.IntegerField
                        };

                var result = q.ToList();
                Assert.AreEqual(1, result.Count);
                var r = result.First();
                Assert.AreEqual(true, r.Submitter == "Parker");
                Assert.AreEqual(true, r.IntegerField == 2);
                Assert.AreEqual(true, r.CheckBoxField == RegularForm.CheckBoxField_Enum.Default2);
            }
        }

        [TestMethod]//select clause
        public void LinqToAR_Count_Clause_With_Select_Clause4()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
            var q = from s in rgf
                    where s.CheckBoxField == RegularForm.CheckBoxField_Enum.Default2
                    && s.IntegerField == 2
                    select new SelectTarget
                    {
                        str1 = s.Submitter,
                        Int1 = s.IntegerField
                    };

            var result = q.ToList();
            Assert.AreEqual(1, result.Count);
            var r = result.First();
            Assert.AreEqual(true, r.str1 == "Parker");
            Assert.AreEqual(true, r.Int1 == 2);
        }

        [TestMethod]//select clause
        public void LinqToAR_Count_Clause_With_Select_Clause5()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
            var q = from s in rgf
                    where s.CheckBoxField == RegularForm.CheckBoxField_Enum.Default2
                    && s.IntegerField == 2
                    select new
                    {
                        str1 = s.Submitter,
                        Int1 = s.IntegerField
                    };

            var result = q.ToList();
            Assert.AreEqual(1, result.Count);
            var r = result.First();
            Assert.AreEqual(true, r.str1 == "Parker");
            Assert.AreEqual(true, r.Int1 == 2);
            //Assert.AreEqual(true, r.CheckBoxField == RegularForm.CheckBoxField_Enum.Default2);
        }

        [TestMethod]//select clause
        public void LinqToAR_Count_Clause_With_Select_Clause6()
        {
            //ignore
            ARLoginContext context = new ARLoginContext("vs31srm01", "jibinw.admin", "admin");
            ARSet<RegularForm> rgf = new ARSet<RegularForm>(context, new ARServerFactory2());
            var q = from s in rgf
                    where s.CheckBoxField == RegularForm.CheckBoxField_Enum.Default2
                    && s.IntegerField == 2
                    select new RegularForm
                    {
                        Submitter = s.Submitter,
                        IntegerField = s.IntegerField
                    };

            var result = q.ToList();
            Assert.AreEqual(1, result.Count);
            var r = result.First();
            Assert.AreEqual(true, r.Submitter == "Parker");
            Assert.AreEqual(true, r.IntegerField == 2);
            //Assert.AreEqual(true, r.CheckBoxField == RegularForm.CheckBoxField_Enum.Default2);
        }



        private class SelectTarget
        {
            public string str1 { get; set; }
            public int Int1 { get; set; }
        }

    }

}
